 const url_all = {
	// 'DEV': 'http://192.168.8.130:8080', // 开发
	'DEV': 'http://139.196.40.231:9090', //订餐管理接口
	'PRO': 'http://139.196.40.231:9090', // 生产
}
export let BASEURL = url_all['DEV'] // 调整当前环境

/*
* 全局请求封装
* @param path 请求路径
* @param method 请求类型(GET/POST/DELETE等)
* @oaram data 请求体数据
* @param loading 请求未完成是是否显示加载中，默认为true
*/
export default (path, method, data = {}, contentType="application/json",loading = true) => {
	// 获取存储token
	const token = uni.getStorageSync("token");
	if (loading) {
		uni.showLoading({
			
			title: "加载中",
			mask: true
		});
	};
	//根据token进行调用函数
	if (token != '') {
		return tokenRequest(path, method, data,contentType, loading, token)
	} else {
		return noTokenRequest(path, method, data,contentType, loading)
	}
};

// 无token时发送请求函数
function noTokenRequest(path, method, data,contentType,loading) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASEURL + path,
			method: method,
			data,
			header:{
				'Content-Type':contentType
			},
			success(response) {
	
				if (response.data.code === 401) {
					uni.navigateTo({
						url:"/pages/login/login"
					})
				}
				// console.log('%c响应拦截：', ' background:green', response);
				/* if (response.data.code === 3001) {
					// logout()
				} */
				/* if (response.data.code !== 20) {
					uni.showToast({
						icon: "none",
						duration: 4000,
						title: response.data.msg
					});
				} */
				console.log(response.data)
				resolve(response.data);
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(err);
				reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}


// 有token时发送请求函数
function tokenRequest(path, method, data, contentType,loading, token) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASEURL + path,
			method: method,
			data,
			header: {
				"Authorization":  token,
				'Content-Type':contentType
			},
			success(response) {
				if (response.data.code === 401) {
					// logout()
					uni.navigateTo({
						url:"/pages/login/login"
					})
				}
				resolve(response.data);
			},
			fail(err) {
				uni.showToast({
					icon: "none",
					title: '服务响应失败'
				});
				console.error(err);
				reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}
